<?php

namespace App\Providers;

use App\Http\Requests\Request;
use App\Models\Follow;
use App\Models\Reply;
use App\Models\Section;
use App\Models\TopicBbs;
use App\Models\User;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\ServiceProvider;
use App\Models\Remind;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        \Carbon\Carbon::setLocale('zh');
        $section_list=Section::all();
        $topic_number=count(TopicBbs::all());
        $replies_number=count(Reply::all());
        $user_number=count(User::all());
        view()->share('section_list',$section_list);
        view()->share('topic_number',$topic_number);
        view()->share('replies_number',$replies_number);
        view()->share('user_number',$user_number);
        view()->composer('*',function($view){
            if (Session::has('userModel')){
                $remind_msg=count(Remind::where('another_id',Session::get('userModel')->id)->where('desc','answer')->where('read',0)->get());
                $user_id=Session::get('userModel')->id;
                $follow=Follow::where('user_id',$user_id)->get();
                $a=[];
                foreach ($follow as $f){
                    $a[]=$f->another->id;
                }
                $remind_topic=[];
                foreach ($a as $b){
                    $remind_topic[]=Remind::where('user_id',$b)->where('desc','created_topic')->where('read',0)->orderBy('created_at','desc')->with('topic','user')->get();
                }
                $f=[];
                foreach ($remind_topic as $a){
                    foreach ($a as $b){
                        $t=Follow::where('user_id',$user_id)->where('another_id',$b->user->id)->get();
                        if ($b->created_at>$t[0]->created_at){
                            $f[]=$b;
                        }
                    }
                }
                $topic_num=count($f);
                $remind_num=$topic_num+$remind_msg;
                $view->with('remind',$remind_num);
            }else{
                $view->with('remind',0);
            }
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
